package com.controller;

import com.entity.Device;
import com.entity.Paging;
import com.entity.User;
import com.entity.Vehicle;
import com.github.pagehelper.Page;
import com.service.ActivityDevService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ActivityDevController {
    @Autowired
    private ActivityDevService activityDevService;

    @PostMapping("addVehicle")
    public String addVehicle(@RequestBody List<Vehicle> vehicles) {

        boolean state = activityDevService.addVehicle(vehicles);

        return "{\"state\":" + state + "}";
    }

    @PostMapping("addDevice")
    public String addDevice(@RequestBody List<Device> devices) {
        System.out.println(devices);
        int state = activityDevService.addDevice(devices);
        return "{\"state\":" + state + "}";
    }

    /*
    查询所有车辆
     paging{
        page:页数
        limit:多少条数据
    }
     */
    @PostMapping("queryVehicles")
    public Object queryVehicles(@RequestBody Paging paging ) {
      List<Vehicle>  vehicles =activityDevService.selectVehicles(paging);
      Page<Vehicle> page = (Page<Vehicle>)vehicles;
      System.out.println(page);

  //    return  json;
      return "{\"total\":"+page.getTotal()+",\"vehicles\":"+JSONArray.fromObject(vehicles)+"}";
    }


    /*
    查询所有设备
     paging{
        page:页数
        limit:多少条数据
    }
     */
    @PostMapping("queryDevs")
    public Object queryDevs(@RequestBody Paging paging ) {
        List<Device>  devices =activityDevService.sleectDevices(paging);
        Page<Device> page = (Page<Device>)devices;

        return "{\"total\":"+page.getTotal()+",\"devices\":"+JSONArray.fromObject(devices)+"}";

    }

    /*
    查询活动车辆
        {
        proID:活动id
        }
     */
    @PostMapping("selectVehiclesByproID")
    public Object selectVehiclesByproID(@RequestBody String data) {
        int id = (int) JSONObject.fromObject(data).get("proID");
        return activityDevService.selectVehiclesByproID(id);
    }

    @PostMapping("deleteVehicle")
    //public Object deleteVehicle(@RequestBody Vehicle vehicle) {
     public Object deleteVehicle(@RequestBody List<Vehicle> list){
        Object object = activityDevService.deleteVehicle(list);
        return object;
    }

    @PostMapping("deleteDev")
    public Object deleteDev(@RequestBody String data) {
        List<String> list = (List<String>) JSONObject.fromObject(data).get("equipId");
        Object object = activityDevService.deleteDev(list);
        return object;
    }

    /*
        设备input查询
        parameter：查询的信息
     */
    @GetMapping("inputDevices")
    public Object inputDevices(String parameter){

        return activityDevService.inputDevices(parameter);
    }
    /*
       车辆input查询
       name：司机名字
    */
    @GetMapping("inputVehicles")
    public Object inputVehicles(String name){
        return activityDevService.inputVehicles(name);
    }

}
